package auditoryUIbk;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.awt.*;

public class JZones extends JComponent implements MouseInputListener {

   Shape[] zones;
   ZoneListener listener;
   int currentzone = -1;

   public JZones(int n, ZoneListener listener) {
      super();
      this.listener = listener;
      zones = new Shape[n];
      for (int i=0; i<n; i++) {
         zones[i] = new Rectangle(20 + 40*i, 20, 40, 40);
      }
      addMouseListener(this);
      addMouseMotionListener(this);
   }
   
   public Dimension getPreferredSize() {
      return new Dimension(40*zones.length + 40, 80);
   }
   
   public void paint(Graphics g) {
      Graphics2D g2 = (Graphics2D)g;
      for (int i=0; i<zones.length; i++) {
         g2.draw(zones[i]);
      }
   }
   
   public int pick(int x, int y) {
      for (int i=0; i<zones.length; i++) {
         if(zones[i].contains(x, y))
            return i;
      }
      return -1;
   }
   
   public void mousePressed(MouseEvent e) {
      currentzone = pick(e.getX(), e.getY());
      listener.fingerPressed(currentzone);
   }

   public void mouseClicked(MouseEvent e) {
   }

   public void mouseReleased(MouseEvent e) {
      listener.fingerReleased(currentzone);
   }

   public void mouseDragged(MouseEvent e) {
      int z = pick(e.getX(), e.getY());
      if (z != currentzone) {
          currentzone = z;
          listener.zoneChanged(z);
      }
   }

   public void mouseMoved(MouseEvent e) {
   }
  
   public void mouseExited(MouseEvent e) {
   }
  
   public void mouseEntered(MouseEvent e) {
   }
}